﻿@using RMS.EF.Model
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    var programs = ViewBag.Programs as List<RMS_Program>;
}

<link href="~/bower_components/bootstrap-treeview-master/bootstrap-treeview.min.css" rel="stylesheet" />
<div class="panel-body">
    <div class="row">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a data-toggle="collapse" data-parent="#accordion" href="#main-search">搜索栏</a>
                </h4>
            </div>
            <div id="main-search" class="panel-collapse collapse in">
                <div class="panel-body">
                    <form id="searchForm" class="form-inline" role="form" onsubmit="search(); return false;">
                        <div class="form-group">
                            <label for="programId">程序</label>
                            <select id="programId" name="programId" class="form-control">
                                @for (int i = 0; i < programs.Count; i++)
                            {
                                    <option value="@programs[i].Id">@programs[i].Name</option>
                            }
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="searchValue">搜索</label>
                            <input type="text" class="form-control" id="searchValue" name="searchValue" maxlength="50" placeholder="名称或链接" />
                        </div>
                        <div class="form-group">
                            <label for="status">状态</label>
                            <select id="status" name="status" class="form-control">
                                <option value="@(E_Status.All)">--全部--</option>
                                <option value="@(E_Status.Disable)">不可用</option>
                                <option value="@(E_Status.Enable)">可用</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="type_m">类型</label>
                            <select id="type_m" name="type_m" class="form-control">
                                <option value="@(E_Type_Menu.All)">--全部--</option>
                                <option value="@(E_Type_Menu.Menu)">菜单</option>
                                <option value="@(E_Type_Menu.Function)">功能</option>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary">搜索</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div id="toolbar" class="btn-group" role="group" style="margin-bottom:10px;">
            <button type="button" class="btn btn-success" onclick="add()">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
            </button>
            <button type="button" class="btn btn-info" onclick="edit()">
                <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>编辑
            </button>
            <button type="button" class="btn btn-danger" onclick="del()">
                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
            </button>
        </div>
    </div>
    <div class="row">
        <div class="dataTable_wrapper">
            <div id="mainTree"></div>
        </div>
    </div>
</div>

@section scripts{
    <script src="~/bower_components/bootstrap-treeview-master/bootstrap-treeview.min.js"></script>
    <!--文字信息-->
    <script>
        var tableurl = "@Url.Action("ListGetData")";
        var delurl = "@Url.Action("Delete")";
        var editurl = "@Url.Action("Edit")";
        var enable = "@((int)E_Status.Enable)";
        var type_menu = "@((int)E_Type_Menu.Menu)";
        var type_function = "@((int)E_Type_Menu.Function)";
        var msg = {
            sel: "请选择一条数据",
            delconfirm: "您确定要删除这条数据吗？",
            add: "新增菜单",
            edit: "编辑菜单",
            multidelconfirm: "您确定要删除这些数据吗？"
        };
    </script>
    <script>
        //树形视图初始化
        $(function () {
            search();
        });
        //搜索
        function search() {
            loading.start();
            var data = {};
            $("#searchForm").serializeArray().map(function (x) { data[x.name] = x.value; });
            $.ajax({
                url: tableurl,
                data: data,
                type: "POST",
                dataType: "JSON",
                success: function (treedata) {
                    if (!treedata.IsSuccess) {
                        toastr.error(treedata.Message);
                        return;
                    }
                    var d = $.arraytotree(treedata.Data, {
                        id: "Id",
                        text: "Name",
                        parentId: "ParentId",
                        creatNode: function (i, item) {
                            if (item.Url) {
                                item.Name = item.Name + ' [' + item.Url + ']';
                            }
                            var color = "red", icon = "";
                            if (item.Status == enable) {
                                color = "green";
                            }
                            switch (item.Type.toString()) {
                                case type_menu:
                                    icon = '&nbsp;<span class="glyphicon glyphicon-tasks" style="color:' + color + '"></span>';
                                    break;
                                case type_function:
                                    icon = '&nbsp;<span class="glyphicon glyphicon-link" style="color:' + color + '"></span>';
                                    break;
                                default:
                                    break;
                            }
                            item.Name = icon + item.Name;
                        }
                    });
                    $("#mainTree").treeview({
                        levels: 2,
                        data: d
                    });
                },
                error: function (xhr, status, error) {
                    alert("error");
                },
                complete: function () {
                    loading.end();
                }
            });
        }
        //删除
        function del() {
            var selectedItem = $("#mainTree").treeview('getSelected');
            if (selectedItem.length <= 0) {
                toastr.warning(msg.sel);
                return;
            }
            $.confirmAjax(msg.delconfirm, { url: delurl, data: { id: selectedItem[0].Id }, search: function () { search(); } });
        }
        //新增
        function add() {
            var selectedItem = $("#mainTree").treeview('getSelected');
            var pid, pgid;
            if (selectedItem.length > 0) {
                pid = selectedItem[0].Id;
            }
            pgid = $("#programId").val();
            $.modalBox(msg.add, { url: editurl, data: { parentId: pid, programId: pgid }, search: function () { search(); } });
        }
        //修改
        function edit() {
            var selectedItem = $("#mainTree").treeview('getSelected');
            if (selectedItem.length <= 0) {
                toastr.warning(msg.sel);
                return;
            }
            $.modalBox(msg.edit, { url: editurl, data: { id: selectedItem[0].Id }, search: function () { search(); } });
        }
    </script>
}

